<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
        $this->display();
    }
    /*
     * 管理员登陆
     */
    public function login(){
        if(session('?admin_id') && session('admin_id')>0){
             $this->error("您已登录",U('Admin/Index/index'));
        }
      
        if(IS_POST){
            $verify = new Verify();
            if (!$verify->check(I('post.vertify'), "admin_login")) {
                exit(json_encode(array('status'=>0,'msg'=>'验证码错误')));
            }
            $condition['user_name'] = I('post.username');
            $condition['password'] = I('post.password');
            if(!empty($condition['user_name']) && !empty($condition['password'])){
                $condition['password'] = encrypt($condition['password']);
                $admin_info = M('admin')->join('__ADMIN_ROLE__ ON __ADMIN__.role_id=__ADMIN_ROLE__.role_id')->where($condition)->find();
                if(is_array($admin_info)){
                    session('admin_id',$admin_info['admin_id']);
                    session('act_list',$admin_info['act_list']);
                    $last_login_time = M('admin_log')->where("admin_id = ".$admin_info['admin_id']." and log_info = '后台登录'")->order('log_id desc')->limit(1)->getField('log_time');
                    M('admin')->where("admin_id = ".$admin_info['admin_id'])->save(array('last_login'=>time(),'last_ip'=>  getIP()));
                    session('last_login_time',$last_login_time);                            
                    adminLog('后台登录',__ACTION__);
                    $url = session('from_url') ? session('from_url') : U('Admin/Index/index');
                    exit(json_encode(array('status'=>1,'url'=>$url)));
                }else{
                    exit(json_encode(array('status'=>0,'msg'=>'账号密码不正确')));
                }
            }else{
                exit(json_encode(array('status'=>0,'msg'=>'请填写账号密码')));
            }
        }
        
        $this->display();
    }
    /*
	 * 登录处理方法 -jack
     */
    public function Login_do(){
    	$data=I('post.');
        $username=trim($data["username"]);
        $password=md5(trim($data["password"]));
    	$M = M("admin_member");
        $data=$M->where("username='$username' and password='$password'")->select();
        $role_id=$data[0]["role_id"];
        $state=$data[0]["state"];
        $id=$data[0]["id"];
        if(empty($data)){
            $this->error(用户名与密码不匹配);
        }else{
            if($state==0){
                $this->error(你已被禁用，请联系管理员);
            }else{
               
               $last_login=time();
               $M=M();
               $sql="insert into y_admin_log (admin_id,log_info,log_time) values ('$id','后台登录','$last_login')";
               $data=$M->execute($sql);
               $this->success(登录成功,U('Admin/Index/index'));
            }
        }
        session_start();
        $_SESSION["admin_member"]=$username;
        $_SESSION["id"]=$id;
        $_SESSION["role_id"]=$role_id;

    }
    /*
     * 退出处理方法 -jack
     */
    public function logout(){
        setcookie('username','',time()-3600,'/');
        setcookie('cn','',time()-3600,'/');
        setcookie('user_id','',time()-3600,'/');
        session_unset();
        session_destroy();
        //$this->success("退出成功",U('Home/Index/index'));
        redirect(U("Login/index"),0,"页面跳转中···");
        exit;
    }
}